import { getToken, setToken,removeToken } from '@/utils/auth'

const state = {
  token: getToken(),
  user:JSON.parse(localStorage.getItem("user"))
}

const mutations = {
  // 存入缓存
  setToken(state,token) {
    state.token = token
    setToken(token)
  },
  removeToken(state) {
    // 从缓存中删除
    state.token = null
    removeToken()
  },
  setUser(state,info){
    state.user = info
    // 存储缓存
    localStorage.setItem("user",JSON.stringify(info))
  },
  removeUser(state,info){

    state.user = {}

    localStorage.setItem("user",JSON.stringify({}))

  }
}

const actions = {
  // context上下文，传入参数
  login(context,data) {
    context.commit('setToken',data)
  },
  setUser(context,data){
    context.commit("setUser",data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
